$('#search_stock_optional').linkbutton({  //查询按钮
    iconCls: "icon-search"
});


function removeOptional(id, stock_code) {
    $.messager.confirm('提示', '确定删除此数据吗？', function (r) {
        if (r) {
            $.ajax({
                /*type = "get",*/
                url: '/optional/remove',
                dateType: "JSON",
                data: {"stock_code": stock_code},
                success: function (data, textStatus, jqXHR) {
                    $.messager.show({
                        title: '提示',
                        msg: data,
                        showType: 'fade',      //设置显示类型
                        style: {left: 500, top: 100},     //设置弹框的位置
                        width: 100,                           //设置弹框的宽度和高度
                        height: 200,
                        timeout: 1000      //设置停留时间，1000毫秒
                    });
                    optional_flush.search();
                }
            });
        } else {
            return;
        }
    });

}

var optional_flush = {                     //obj对象
    search: function () {
        $('#table_stock_optional').datagrid('load', $("#form_stock_optional").parseForm());
    }
};
$(function () {


    $('#search_stock_optional').click(function () {
        optional_flush.search()
    });


    $('#table_stock_optional').datagrid({
        // width: 1500,                 //设置宽度
        url: '/optional/stock-list',       //远程加载数据地址
        title: '股票池',           //面板属性，添加标题
        iconCls: 'icon-search',     //添加图标
        method: 'get',
        queryParams: $("#form_stock_optional").parseForm(),
        selectOnCheck: true,
        remoteSort: true,
        columns: [[
            {
                field: 'id',
                checkbox: true,
                width: 60
            },
            {
                field: 'stock_name',
                title: '股票名称',
                formatter: linkStockDetail
            },
            {
                field: 'industry_name',
                title: '行业名称'
            }

            , {
                field: 'create_time',
                title: '创建时间',
                formatter: dateFormatter
            }
            , {
                title: '操作',
                // resizable: true,
                field: '获取最新',
                width: 60,
                formatter: formatOper
            }
        ]],
        pagination: true,         //组件底部显示分页工具栏
        pageNumber: 1,             //设置分页时初始化页码
        pageSize: 20,               //设置分页时设置每页多少条
        pageList: [20, 50, 100, 500],          //设置可选每页显示条数
        fitColumns: true,         //设置表格自适应
        // {#scrollbarSize: 0,          //滚动条所占的宽度或高度。默认为18。#}
        rownumbers: true,

        toolbar: '#toolbar_stock_optional'
    });

    /*  $('#stockinfo_search').linkbutton({  //查询按钮
          iconCls: "icon-search"
      });*/


});

function formatOper(val, row, index) {
    let holdState = row.hold_state;
    let lineState = row.line_state;
    html = "<button class='easyui-linkbutton' onclick=\"query_lifeline('" + row.stock_code + "')\">查看当前生命线</button>" +
        " <button class='easyui-linkbutton' onclick=\"removeOptional('" + row.id + "','" + row.stock_code + "')\">移除自选股</button> "
    if (holdState == 0) {
        html += "<button class='easyui-linkbutton' onclick=\"buy_stock('" + row.id + "','" + row.stock_name + "')\">购买</button> "
    }
    if (lineState == 0) {
        html += "<button class='easyui-linkbutton' onclick=\"create_lifeline('" + row.stock_code + "','" + row.stock_name + "')\">生成生命线</button>";
    }
    return html
}

function buy_stock(optional_id, stock_name) {
    $('#optional_id').val(optional_id);
    $('#optional_stock_name').textbox('setValue', stock_name);

    $('#optional_buy_amount').spinner({
        required: true,
        value: 100,
        min: 100,
        max: 1000,
        increment: 100,
        onSpinUp: function () {
            let price = parseInt($("#optional_buy_amount").spinner('getValue'))
            if (price < 2000) {
                price += 100
            }
            $("#optional_buy_amount").spinner('setValue', price);
        },
        onSpinDown: function () {
            let price = parseInt($("#optional_buy_amount").spinner('getValue'))
            if (price > 100) {
                price -= 100
            }
            $("#optional_buy_amount").spinner('setValue', price);
        }
    });


    // $("#optional_stock_name").val(stock_name)
    $('#optional_buy_stock').window('open')
}

function create_lifeline(stock_code, stock_name) {
    $.ajax({
        /*type = "get",*/
        url: '/optional/create_lifelines',
        dateType: "JSON",
        data: {"stock_code": stock_code, "stock_name": stock_name},
        success: function (data, textStatus, jqXHR) {
            optional_flush.search()
        }
    });
}

 function query_lifeline(stock) {
        // 加载股价信息
        $.ajax({
            /*type = "get",*/
            url: '/lifeline/stock-price',
            dateType: "JSON",
            data: {"stock_code": stock},
            success: function (data, textStatus, jqXHR) {
                if (data) {
                    $("#op_ll_stock_code").textbox('setValue', stock);
                    $("#op_ll_stock_name").textbox('setValue', data.stock_name);
                    $("#op_ll_cur_price").textbox('setValue', data.now);
                    $("#op_ll_open").textbox('setValue', data.open);
                    $("#op_ll_high").textbox('setValue', data.high);
                    $("#op_ll_low").textbox('setValue', data.low);

                }


            }
        });

        // 支撑线
        $.ajax({
            /*type = "get",*/
            url: '/lifeline/stock-pressureline',
            dateType: "JSON",
            data: {"stock_code": stock},
            success: function (data, textStatus, jqXHR) {
                if (data) {
                    $("#op_ll_press_id").val(data.id);
                    $("#op_ll_press_price").textbox('setValue', data.point_1_price)
                    $("#op_ll_press_time").textbox('setValue', dateFormatter1(data.point_1_time))

                }
            }
        });

        // 阻力线
        $.ajax({
            /*type = "get",*/
            url: '/lifeline/stock-supportline',
            dateType: "JSON",
            data: {"stock_code": stock},
            success: function (data, textStatus, jqXHR) {
                if (data) {
                    $("#op_ll_support_id").val(data.id);
                    $("#op_ll_support_price").textbox('setValue', data.point_1_price);
                    $("#op_ll_support_time").textbox('setValue', dateFormatter1(data.point_1_time));

                }
            }
        });

        // 弹窗
        $('#win_optional_lifeline').window('open'); // open a window
    }
